package com.lhkj.ct.admin.modules.gauge.scale.controller;


import com.lhkj.ct.base.annntation.Log;
import com.lhkj.ct.base.enums.ActionType;
import com.lhkj.ct.base.model.ro.ActionResult;
import com.lhkj.ct.base.utils.Asserts;
import com.lhkj.ct.meta.modules.gauge.common.enums.GaugeType;
import com.lhkj.ct.meta.modules.gauge.model.dto.QuestionsDto;
import com.lhkj.ct.meta.modules.gauge.model.entity.TblGaugeQuestion;
import com.lhkj.ct.meta.modules.gauge.model.entity.TblScale;
import com.lhkj.ct.meta.modules.gauge.service.GaugeQuestionService;
import com.lhkj.ct.meta.modules.gauge.service.ScaleConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jobob
 * @since 2023-10-08
 */
@RestController
@RequestMapping("api/plat/gauge/scale/question")
@Api(tags = {"6.1.2题目配置"})
@Validated
public class ScaleQuestionController {

    @Resource
    private GaugeQuestionService questionService;

    @Resource
    private ScaleConfigService scaleConfigService;

    @GetMapping(value = "{gaugeId}/list")
    @ApiOperation(value = "量表题目数据")
    //@RequiresPermissions(value = {"system:scale:insert", "system:scale:update"}, logical = Logical.OR)
    public ActionResult<List<TblGaugeQuestion>> list(@PathVariable String gaugeId) {
        TblScale scale = scaleConfigService.getById(gaugeId);
        Asserts.assertFalse(null == scale, "量表不存在");
        return ActionResult.success(questionService.selectQutWithOptWithLogicByGaugeId(scale.getId(), GaugeType.SCALE));
    }

    @GetMapping(value = "{gaugeId}/info")
    @ApiOperation(value = "量表题目json")
    //@RequiresPermissions(value = {"system:scale:insert", "system:scale:update"}, logical = Logical.OR)
    public ActionResult<String> questionJson(@PathVariable String gaugeId) {
        TblScale scale = scaleConfigService.getById(gaugeId);
        Asserts.assertFalse(null == scale, "量表不存在");
        return ActionResult.success(scale.getQuestionJson());
    }

    @PostMapping(value = "{gaugeId}/save")
    @ApiOperation(value = "保存题目和逻辑信息")
    @Log(title = "量表题目配置", actionType = ActionType.UPDATE)
    //@RequiresPermissions(value = {"system:scale:insert", "system:scale:update"}, logical = Logical.OR)
    public ActionResult<List<TblGaugeQuestion>> save(@PathVariable String gaugeId,@Validated @RequestBody QuestionsDto questionsDto) {
        TblScale scale = scaleConfigService.getById(gaugeId);
        Asserts.assertFalse(null == scale, "量表不存在");
        questionService.saveByGauge(gaugeId, GaugeType.SCALE, questionsDto);
        return ActionResult.success();
    }

}
